<template>
    <app-dialog v-model="show" width="600px" style="top: 3vh" title="转账" @open="handleOpen">
        <el-form class="activation_form" :model="formdata" label-width="150px" ref="ruleFormRef" label-position="right"
            :rules="rules">
            <el-form-item label="钱包地址:" prop="to_token">
                <el-input v-model="formdata.to_token" style="width: 300px" placeholder="目标钱包地址" />
            </el-form-item>
            <el-form-item label="转账金额:" prop="amount">
                <el-input-number v-model="formdata.amount" class="mx-4" :min="50" style="width: 300px"
                    :max="numdata.KZZ" controls-position="right" />
            </el-form-item>
            <el-form-item label="密码:" prop="pass_word_b">
                <el-input v-model="formdata.pass_word" type="password" style="width: 300px" placeholder="请输入密码" />
            </el-form-item>
        </el-form>

        <template #footer>
            <el-button @click="handleClose">取消</el-button>
            <el-button type="primary" class="hovepcvicecolor" @click="handleSubmit()">确定</el-button>
        </template>
    </app-dialog>
</template>

<script setup>
import http from '@/utils/uc-http'
import { ElMessage } from 'element-plus'

const props = defineProps({
    numdata: Object
})
const show = defineModel({ type: Boolean })
const emits = defineEmits(['change'])
const userStore = useUserStore()
const ruleFormRef = ref(null)
const formdata = ref({
    to_token: '',
    amount: 50,
    pass_word: ''
})

const rules = reactive({
    to_token: [{ required: true, message: '请输入目标钱包地址', trigger: 'blur' }],
    pass_word: [{ required: true, message: '请输入转账密码', trigger: 'blur' }],
})

const handleOpen = () => {
    formdata.value = {
        to_token: '',
        amount: 50,
        pass_word: ''
    }
}

const handleClose = () => {
    show.value = false
}
const handleSubmit = () => {
    ruleFormRef.value.validate(valid => {
        if (valid) {
            let body = JSON.parse(JSON.stringify(formdata.value))
            body.from_token = userStore.user.bank
            body.type = 'KZZ'
            console.log(body);

            http.post('/transfer', body).then((res) => {
                ElMessage.success('转账成功')
                userStore.checkInfo()
                handleClose()
                emits('change')
            })

        }
    })
}
</script>

<style lang="scss" scoped></style>

<style>
.activation_form .el-input__wrapper {
    border-radius: 0;
}
</style>